/*
 * Copyright (c) 2008, Artur Emagulov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * 27.08.2008
 * arch.h
 */

#ifndef _ARCH_H
#define _ARCH_H

#include <sys/types.h>

/**
 * Common register frame for trap/interrupt.
 * These cpu state are saved into top of the kernel stack in
 * trap/interrupt entries. Since the arguments of system calls are
 * passed via registers, the system call library is completely
 * dependent on this register format.
 *
 * The value of ss & esp are not valid for kernel mode trap
 * because these are set only when privilege level is changed.
 */
struct cpu_regs {
	uint32_t ebx;		/*  +0 (00) --- s/w trap frame --- */
	uint32_t ecx;		/*  +4 (04) */
	uint32_t edx;		/*  +8 (08) */
	uint32_t esi;		/* +12 (0C) */
	uint32_t edi;		/* +16 (10) */
	uint32_t ebp;		/* +20 (14) */
	uint32_t eax;		/* +24 (18) */
	uint32_t ds;		/* +28 (1C) */
	uint32_t es;		/* +32 (20) */
	uint32_t trap_no;	/* +36 (24) --- h/w trap frame --- */
	uint32_t err_code;	/* +40 (28) */
	uint32_t eip;		/* +44 (2C) */
	uint32_t cs;		/* +48 (30) */
	uint32_t eflags;	/* +52 (34) */
	uint32_t esp;		/* +56 (38) */
	uint32_t ss;		/* +60 (3C) */
};

/**
 * Kernel mode context for context switching.
 */
struct kern_regs {
	uint32_t eip;		/*  +0 (00) */
	uint32_t ebx;		/*  +4 (04) */
	uint32_t edi;		/*  +8 (08) */
	uint32_t esi;		/* +12 (0C) */
	uint32_t ebp;		/* +16 (10) */
	uint32_t esp;		/* +20 (14) */
};

#ifdef CONFIG_FPU
/**
 * FPU register for fsave/frstor
 */
struct fpu_regs {
	uint32_t ctrl_word;
	uint32_t stat_word;
	uint32_t tag_word;
	uint32_t ip_offset;
	uint32_t cs_sel;
	uint32_t op_offset;
	uint32_t op_sel;
	uint32_t st[20];
};
#endif

/**
 * Processor context
 */
struct context {
	u_int				pl;				/* privelege level */
	vaddr_t				k_stack_top;	/* kernel stack top */
	vaddr_t				u_stack_top;	/* user stack top */
	struct kern_regs	kregs;		/* kernel mode registers */
	struct cpu_regs		*uregs;		/* user mode registers */
	struct cpu_regs		*saved_regs;	/* saved user mode registers */
#ifdef CONFIG_FPU
	struct fpu_regs		*fregs;		/* co-processor registers */
#endif
	uint32_t	 esp0;		/* top of kernel stack */
};

typedef struct context *context_t;	/* context id */

/**
 * x86 PIC interrupts (Remapped)
 */
#if 0
	#define ARCH_TS_PEN_INT	28	/* Touchscreen pen-down event interrupt */
	#define ARCH_ETH_INT		27	/* Ethernet interface interrupt */
	#define ARCH_CPPLD_INT	26	/* Interrupt from secondary interrupt controller, see Secondary interrupt controller registers. */
	#define ARCH_AACI_INT		25	/* Audio interface interrupt */
	#define ARCH_MMCI1_INT	24	/* MultiMedia card interface 1 */
	#define ARCH_MMCI0_INT	23	/* MultiMedia card interface 0 */
	#define ARCH_CLDC_INT		22	/* Display controller interrupt */
	#define ARCH_LM_LL1_INT	10	/* Logic module low-latency interrupt 1 */
	#define ARCH_LM_LL0_INT	9	/* Logic module low-latency interrupt 0 */
	#define ARCH_RTC_INT		8	/* Real time clock interrupt */
	#define ARCH_TIMER2_INT	7	/* Counter-timer 2 interrupt */
	#define ARCH_TIMER1_INT	6	/* Counter-timer 1 interrupt */
#endif
	#define ARCH_TIMER0_INT	0	/* Counter-timer 0 interrupt */
#if 0
	#define ARCH_MOUSE_INT	4	/* Mouse interrupt */
#endif
	#define ARCH_KBD_INT		1	/* Keyboard interrupt */
#if 0
	#define ARCH_UART1_INT	2	/* UART 1 interrupt */
	#define ARCH_UART0_INT	1	/* UART 0 interrupt */
	#define ARCH_SOFT_INT		0	/* Software interrupt */
#endif

	/* Which interrupt is used for periodic scheduling */
	#define ARCH_PERIODIC_INT	ARCH_TIMER0_INT

/* This returns interrupt vector number.
 * This called by
 */
__inline__ uint32_t __arch_get_ivecno (struct cpu_regs *);

#if 0
/* types for context_set */
#define CTX_KSTACK	0		/* set kernel mode entry address */
#define CTX_KENTRY	1		/* set kernel mode entry address */
#define CTX_KARG		2		/* set kernel mode argument */
#define CTX_USTACK	3		/* set user mode stack address */
#define CTX_UENTRY	4		/* set user mode entry addres */
#define CTX_UARG		5	  	/* set user mode argument */

extern void	context_set(context_t, int, vaddr_t);
extern void	context_switch(context_t, context_t);
extern void	context_save(context_t);
extern void	context_restore(context_t);

/*
 * Memory Management Unit
 */
typedef uint32_t *pgd_t;			/* page directory */

/* memory page type */
#define PG_UNMAP	0		/* no page */
#define PG_READ		1		/* read only */
#define PG_WRITE	2		/* read/write */

#ifdef CONFIG_MMU
extern void	 mmu_init(void);
extern pgd_t	 mmu_newmap(void);
extern void	 mmu_delmap(pgd_t);
extern int	 mmu_map(pgd_t, void *, void *, size_t, int);
extern void	 mmu_switch(pgd_t);
extern void	*mmu_extract(pgd_t, void *, size_t);
#else /* CONFIG_MMU */
#define mmu_init()		do {} while (0)
#endif /* CONFIG_MMU */

#endif


#if 0
/*
 * User Memory access
 */
extern int	 umem_copyin(const void *, void *, size_t);
extern int	 umem_copyout(const void *, void *, size_t);
extern int	 umem_strnlen(const char *, size_t, size_t *);

extern void	 breakpoint(void);

#endif /* if 0 */

#endif /* !_ARCH_H */
